import React from 'react';
import logFactory from "log/LogFactory";
import useAsyncEffect from "hooks/useAsyncEffect";
import {Form, Select} from "antd";
import {DefaultOptionType} from "rc-select/lib/Select";
import {Part} from "type/Part";

import DrpSettingHandler from "../DrpSettingHandler";
import {getProviderOptions} from "../SeleteOptionHanlder";
import {PartFormSelectProps} from "../init";

const log = logFactory.getLog("PartProviderSelect");

/**
 * 零件供应商选择器
 */
const PartProviderSelect = ({ form,defaultValue,disableValue }: PartFormSelectProps) => {
    const [loading,setLoading] = React.useState<boolean>(true) ;
    const [options,setOptions] = React.useState<DefaultOptionType[]>([]) ;
    useAsyncEffect( async ()=>{
        setOptions(await getProviderOptions()) ;
        if (!disableValue){
            defaultValue = defaultValue || DrpSettingHandler.getInstance().getConfig().defaultProvider ;
            form.setFieldValue("partProvider",defaultValue) ;
        }
        setLoading(false) ;
    }) ;

    return (
        <Form.Item<Part> name={"partProvider"} label="零件供应商" required>
            <Select loading={loading} options={options}/>
        </Form.Item>
    );
};

export default PartProviderSelect;